package com.atguigu.base;

import com.atguigu.util.CastUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-07-19 15:02
 * @Description:
 */

@Transactional
public  abstract class BaseServiceImpl<T> implements BaseService<T> {

    protected abstract BaseDao<T> getEntityDao();


    @Override
    public void insert(T role) {
        getEntityDao().insert(role);
    }

    @Override
    public T getById(Serializable id) {
        T role = getEntityDao().getById(id);
        return role;
    }

    @Override
    public void update(T role) {
        getEntityDao().update(role);
    }

    @Override
    public void delete(Serializable id) {
        getEntityDao().delete(id);
    }

    @Override
    public PageInfo<T> findPage(Map<String, Object> filters) {
        int pageNum = CastUtil.castInt(filters.get("pageNum"),1);
        int pageSize = CastUtil.castInt(filters.get("pageSize"),5);

        //启动分页插件
        //select count(*)...获取符号查询条件的记录总数
        //给查询条件添加limit子句
        PageHelper.startPage(pageNum,pageSize);

        //查询当前页的数据
        Page<T> page = getEntityDao().findPage(filters);

        //封装数据并返回
        return new PageInfo<>(page);
    }
}
